/**
 * Created by Administrator on 2018-02-05.
 */

var Sudoku = function () {

    var arr, difficulty = 1;
    var row, col, old;
    var model = 0;
    var timer = new TimerUtil($("#time"), 50), timeState = true;

    /*玩法模式切换*/
    var modelChange = function () {
        var _this = $(this);
        var input = $(".input");
        if(model === 0){
            model = 1;
            _this.text("输入模式");
            input.prop("readonly", true);
            input.unbind("change");
            input.on("click", onclick);
        }else {
            model = 0;
            _this.text("按钮模式");
            input.prop("readonly", false);
            input.unbind("click");
            input.on("change", onInput);
        }
    };

    /*输入模式*/
    var onInput = function () {
        if(timeState)
            timer.start();
        var _this = $(this);
        var val = _this.val();
        var reg = /^[0-9]*$/;
        if(!reg.test(val)){
            _this.val('');
            val = '';
        }
        onchange(_this, parseInt(val));
    };

    /*按钮模式*/
    var onclick = function () {
        if(timeState)
            timer.start();
        var _this = $(this);
        var val = parseInt(_this.val()) | 0;
        var value;
        if(val === 9) {
            value = 0;
        }else {
            value = val + 1;
        }
        onchange(_this, value);
        _this.val(value === 0 ? '' : value);
    };

    /*棋盘变更检查*/
    var onchange = function (_this, val) {
        row = _this.attr("row") - 1;
        col = _this.attr("col") - 1;
        old = arr[row][col];
        arr[row][col] = val;
        var flag = audit(row, col, val, old);
        flag ? _this.css("color", "blue") : _this.css("color", "red");
    };

    /*棋盘验证*/
    var audit = function (row, col, val, old) {
        if(val === '') return false;
        var br = parseInt(row / 3) * 3;
        var bc = parseInt(col / 3) * 3;
        var flag = true;
        for (var i = 0; i < 9; i++) {
            if(i !== col && arr[row][i] === val) flag = false;
            if(arr[row][i] === old) changeBack(row, i);
            if(i !== row && arr[i][col] === val) flag = false;
            if(arr[i][col] === old) changeBack(i, col);
            if(parseInt(br + i / 3) !== row && parseInt(bc + i % 3) !== col
                && arr[parseInt(br + i / 3)][parseInt(bc + i % 3)] === val) flag = false;
            if(arr[parseInt(br + i / 3)][parseInt(bc + i % 3)] === old)
                changeBack(parseInt(br + i / 3), parseInt(bc + i % 3));
        }
        return flag;
    };

    /*回调验证*/
    var changeBack = function (r, c) {
        var val = arr[r][c];
        var br = parseInt(r / 3) * 3;
        var bc = parseInt(c / 3) * 3;
        var flag = true;
        for(var i = 0; i < 9; i++) {
            if(i !== c && arr[r][i] === val) {
                flag = false;
                break;
            }
            if(i !== r && arr[i][c] === val) {
                flag = false;
                break;
            }
            if(parseInt(br + i / 3) !== r && parseInt(bc + i % 3) !== c
                && arr[parseInt(br + i / 3)][parseInt(bc + i % 3)] === val) {
                flag = false;
                break;
            }
        }
        var e = $(".input[row=" + (r+1) + "][col=" + (c+1) + "]");
        if(flag && e.hasClass("input")){
            e.css("color", "blue");
        }
    };

    /*完成提交*/
    var submit = function () {
        if(submitCheck()) {
            timer.stop();
            timeState = false;
            var input = $(".input");
            if(model === 0){
                input.unbind("change");
            }else {
                input.unbind("click");
            }
            alert("you win!!! congratulations！\n你的成绩：" + timer.toString())
        }else {
            alert("还有空格没有填完，或者还有错误的地方哦！\nplease checked!!!");
        }
    };

    /*完成检查*/
    var submitCheck = function () {
        for(var i = 0; i < 9; i++) {
            var rows = new Array(9), cols = [], blocks = [];
            for(var j = 0; j < 9; j++) {
                if(rows.indexOf(arr[i][j]) > 0) return false;
                rows[j] = arr[i][j];
                if(cols.indexOf(arr[j][i]) > 0) return false;
                cols[j] = arr[j][i];
                if(i % 3 === 0 && j % 3 === 0) {
                    var br = parseInt(i / 3) * 3;
                    var bc = parseInt(j / 3) * 3;
                    for(var k = 0; k < 9; k++) {
                        if (blocks.indexOf(arr[parseInt(br + k / 3)][parseInt(bc + k % 3)]) > 0) return false;
                        blocks[k] = arr[parseInt(br + k / 3)][bc + k % 3];
                    }
                    blocks.splice(0, blocks.length);
                }
            }
            rows.splice(0, rows.length);
            cols.splice(0, cols.length);
        }
        return true;
    };

    /*重置*/
    var reset = function () {
        var inputs = $(".input");
        $.each(inputs, function (i, n) {
            n = $(n);
            row = n.attr("row") - 1;
            col = n.attr("col") - 1;
            arr[row][col] = 0;
            n.val('');
        });
    };

    /*下一局*/
    var next = function () {
        window.location.href = '/games/sudoku?difficulty='+difficulty;
    };

    /*难度设置*/
    var setDifficulty = function () {
        $(".difficulty").prop("disabled", false);
        var _this = $(this);
        _this.prop("disabled", true);
        difficulty = _this.attr('value');
    };

    /*初始化*/
    return {
        init : function (a, d) {
            arr = a;difficulty = d;
            $(".input").on("change", onInput);//默认启动输入模式
            $("#model").on("click", modelChange);//游戏输入模式切换
            $("#reset").on("click", reset);//重置游戏
            $("#submit").on("click", submit);//完成游戏
            $("#next").on("click", next);//下一局
            $(".difficulty").on("click", setDifficulty);//难度设定
        }
    }

}();